package com.samoiloff.ui.list.smart.model {
	import com.samoiloff.ui.list.smart.interfaces.ILayout;

	import flash.display.DisplayObjectContainer;
	import flash.geom.Rectangle;

	/**
	 * @author Samoiloff
	 */
	public class SmartListLayout implements ILayout {
		
		protected var _contentHeight : Number;
		protected var _root : DisplayObjectContainer;
		
		public function SmartListLayout(root:DisplayObjectContainer) {
			_root = root;
			_contentHeight = 0;	
		}

		public function apply(arr:Array, bounds:Rectangle = null): void {
			_contentHeight = 0;
			if (!_root.stage) {
				throw new Error("\"root\" variable should be attached to stage" + _root);
			}
			
			if (arr) {
				for (var i : int = 0; i < arr.length; i++) {
					var cellRenderer : SmartListCellRenderer = arr[i] as SmartListCellRenderer;
					cellRenderer.visible = true;
					var r : Rectangle = cellRenderer.calculateBounds(new Rectangle(0, 0, bounds.width, 0));
					r.y = _contentHeight;
					cellRenderer.bounds = r;
					cellRenderer.visible = false;
					_contentHeight = cellRenderer.bounds.y + cellRenderer.bounds.height;
				}
			}
		}
		
		public function get contentHeight(): Number {
			return _contentHeight;
		}
		
	}
}
